package com.htc.model.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.htc.constants.Constants;
import com.htc.model.dto.FitnessScheduleDTO;

public class FitnessScheduleModel {
	public static FitnessScheduleModel instance;

	public static FitnessScheduleModel getInstance() {
		if (instance == null)
			instance = new FitnessScheduleModel();
		return instance;
	}

	public boolean checkDB(Context context, String id) {
		boolean flag = false;
		String sql = "SELECT fitness_schedule_id FROM "
				+ Constants.TBL_FITNESS_SCHEDULE
				+ " where fitness_schedule_id='" + id + "'";
		DBHelper db = DBHelper.getInstance(context);
		Cursor c = db.Excutequery(sql);
		while (c.moveToNext()) {
			flag = true;
			break;
		}
		c.close();
		db.close();
		return flag;
	}

	public boolean insertFitSche(FitnessScheduleDTO fitc, Context context) {
		boolean flag = false;
		if (!checkDB(context, fitc.getFitnessScheduleId())) {
			String DATABASE_TABLE = Constants.TBL_FITNESS_SCHEDULE;
			ContentValues values = new ContentValues();
			values.put("fitness_schedule_id", fitc.getFitnessScheduleId());
			values.put("fitness_schedule_class_id",
					fitc.getFitnessScheduleClassId());
			values.put("fitness_schedule_instructor_id",
					fitc.getFitnessScheduleInstructorId());
			values.put("fitness_schedule_start_date",
					fitc.getFitnessScheduleStartDate());
			values.put("fitness_schedule_day", fitc.getFitnessScheduleDay());
			values.put("fitness_schedule_start_time",
					fitc.getFitnessScheduleStartTime());
			values.put("fitness_schedule_end_time",
					fitc.getFitnessScheduleEndTime());
			DBHelper db = DBHelper.getInstance(context);
			flag = db.ExcuteInsert(null, DATABASE_TABLE, values);
			db.close();
		}

		return flag;
	}

	public boolean updateFitSche(FitnessScheduleDTO fitc, Context context) {
		boolean flag = false;

		String DATABASE_TABLE = Constants.TBL_FITNESS_SCHEDULE;
		String where = "fitness_schedule_id='" + fitc.getFitnessScheduleId()
				+ "'";
		ContentValues values = new ContentValues();

		values.put("fitness_schedule_class_id",
				fitc.getFitnessScheduleClassId());
		values.put("fitness_schedule_instructor_id",
				fitc.getFitnessScheduleInstructorId());
		values.put("fitness_schedule_start_date",
				fitc.getFitnessScheduleStartDate());
		values.put("fitness_schedule_day", fitc.getFitnessScheduleDay());
		values.put("fitness_schedule_start_time",
				fitc.getFitnessScheduleStartTime());
		values.put("fitness_schedule_end_time",
				fitc.getFitnessScheduleEndTime());
		DBHelper db = DBHelper.getInstance(context);
		flag = db.ExcuteUpdate(null, DATABASE_TABLE, values, where, null);
		db.close();
		return flag;
	}

	public void deleteFitSche(FitnessScheduleDTO fitc, Context context) {
		String sql = "fitness_schedule_id='" + fitc.getFitnessScheduleId()
				+ "'";
		DBHelper db = DBHelper.getInstance(context);
		String DATABASE_TABLE = Constants.TBL_FITNESS_SCHEDULE;
		db.deleteTable(DATABASE_TABLE, sql);
		db.close();
	}
}
